package com.qf.vue2.web.controller;

import com.github.pagehelper.PageInfo;
import com.qf.vue2.pojo.R;
import com.qf.vue2.pojo.Student;
import com.qf.vue2.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/stu")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/{pageNum}/{pageSize}")
    public R findPage(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){
        PageInfo<Student> pageInfo = studentService.findPage(pageNum, pageSize);

        return R.success(pageInfo);
    }

    @PutMapping
    public R update(Student student){
        int count = studentService.update(student);
        if (count > 0){
           return R.success("修改成功");
        }else{
           return R.fail("修改失败");
        }
    }

    @PostMapping
    public R add(@RequestBody Student student){
        int count = studentService.add(student);
        if (count > 0){
           return R.success("添加成功");
        }else{
           return R.fail("添加失败");
        }
    }

    @DeleteMapping("/{number}")
    public R del(@PathVariable("number")String number){
        int count = studentService.del(number);
        if (count > 0){
           return R.success("删除成功");
        }else{
           return R.fail("删除失败");
        }
    }
}
